var tools = require('./tools');
var JsonDataStr = require('./jsonDataStr');
var crypto = require('crypto');
var config = require('../config');

//登录页面
exports.login = function(req, res) {
	res.render('admin/login', {
		title: '后台系统登录',
		companyName: '宽窄说'
	})
}

//登录提交
exports.login_send = function(req, res) {
	var user_name = req.body.user_email;
	var user_pwd = req.body.user_pwd;
	if (user_name == "system" && user_pwd == "123123") {
		var userinfo = {
			"_id": "57b9d9c94e13f10809f4db03",
			"channelid": "0",
			"isAuth": false,
			"job": {
				"__v": 0,
				"_id": "57b9de9c918b2a88077ca89b",
				"isPartTime": false,
				"duty": "cto",
				"editDate": null,
				"JobEndDate": null,
				"JobBeginDate": 1451606400000,
				"isVip": true,
				"JobName": "cto",
				"companyName": "宽窄说",
				"uid": "aaed4d90-67bc-11e6-be28-e3b0273be575"
			},
			"role": 4,
			"loginWay": 0,
			"cardID": null,
			"note": null,
			"entSchoolDate": 1999,
			"degree": null,
			"profession": "机电工程",
			"school": "河南工程学院",
			"createDate": 1471797070058,
			"subscribeDate": 1471797070058,
			"subscribe": true,
			"headimgurl": "/images/user.png",
			"country": "中国",
			"province": "河南",
			"city": "郑州",
			"sex": 1,
			"nickname": "Rainbow",
			"AppId": "wx075465006d2819ea",
			"openid": "oxtgAuE-UjHb2qoAidei1D1oyOEs",
			"uid": "aaed4d90-67bc-11e6-be28-e3b0273be575",
			"__v": 0,
			"email": "by371@163.com",
			"mobile": "13643713298",
			"password": "bf37e6dc6a6023e1a4c103fc6b98e7f6",
			"truename": "系统管理员"
		}
		req.session.sys_user = null;
		req.session.sys_user = userinfo; //将用户信息存为session
		return res.json({
			status: true,
			message: 'login in success',
		})
	}
	return res.json({
		status: false,
		message: "用户名或密码错误",
		code: 1001
	})

	/*var modelId = config.topModelId;
	var email = req.body.user_email;
	var password = req.body.user_pwd;
	var postData = JSON.stringify({
		modelId: modelId,
		'email': email,
		'password': password
	});
	var path = '/v1/manager/user/login';
	JsonDataStr.post(postData, path, function(callback) {
		if (!callback.status) {
			return res.json({
				status: false,
				message: callback.message,
				code: callback.code
			})
		}
		req.session.sys_user = null;
		req.session.sys_user = callback.resources; //将用户信息存为session
		return res.json(callback);
	})*/
}

//首页
exports.homepage = function(req, res) {
	var topModelId = config.topModelId;
	var path = '/v1/manager/report/index';
	var postData = JSON.stringify({
		topModelId: topModelId
	});
	JsonDataStr.post(postData, path, function(row) {
		var resources = {
			allOrders: {
				totalAmount: 0,
				total: 0
			},
			todayOrders: {
				totalAmount: 0,
				total: 0
			},
			allUsers: 0,
			registerUsers: 0,
			allRecharge: 0,
			allPaid: 0,
			todayRecharge: 0,
			todayPaid: 0
		};
		/*if (row.status) {
			resources = row.resources;
		}*/
		res.render('admin/index', {
			title: '后台首页',
			userinfo: req.session.sys_user,
			resources: resources
		});
	})

}

exports.myTodosAjax = function(req, res) {
	res.json({
		status: false,
		message: 'no todos'
	});
}

exports.search = function(req, res) {
	var page = req.query.page;
	var size = req.query.size;
	if (!page) {
		page = 1;
	}
	if (!size) {
		size = 10;
	}
	var keyword = req.query.keyword;
	if (!keyword) {
		return res.json({
			status: false,
			message: 'no keyword'
		});
	}
	var postData = JSON.stringify({
		topModelId: config.topModelId,
		keyWords: keyword
	});
	var path = '/v1/goods/search/title?page=' + page + '&size=' + size;
	JsonDataStr.post(postData, path, function(result) {
		console.log(result)
		if (!result.status) {
			return res.render('admin/msg', {
				title: '搜索',
				userinfo: req.session.sys_user,
				info: result.message,
				btnOKUrl: null,
				btnOKName: null,
				btnCancelUrl: '/admin/index',
				btnCancelName: '返回'
			});
		}
		res.render('admin/search', {
			title: "搜索",
			userinfo: req.session.sys_user,
			goodses: result.doc,
			total: result.total,
			thisPage: page
		});
	});
}